package com.ruoyi.system.service.front.impl;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.front.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.mapper.front.MerchantFrontMapper;
import com.ruoyi.system.mapper.front.SafetyExaminationFrontMapper;
import com.ruoyi.system.service.connectUnity.IConnectService;
import com.ruoyi.system.service.connectUnity.impl.ConnectServiceImpl;
import com.ruoyi.system.service.front.ISafetyExaminationFrontService;
import net.sf.jsqlparser.statement.execute.Execute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class SafetyExaminationFrontServiceImpl extends CxBase implements ISafetyExaminationFrontService {

    @Autowired
    private SafetyExaminationFrontMapper safetyExaminationFrontMapper;
    @Autowired
    private SafetyExaminationStudentRecordMapper safetyExaminationStudentRecordMapper;
    @Autowired
    private MerchantFrontMapper merchantFrontMapper;
    @Autowired
    private SafetyExaminationExaminationpaperMapper safetyExaminationExaminationpaperMapper;
    @Autowired
    private SafetyExaminationQuestionMapper safetyExaminationQuestionMapper;
    @Autowired
    private SafetyExaminationStudentWrongRecordMapper safetyExaminationStudentWrongRecordMapper;
    @Autowired
    private SafetyExaminationPaperInfoMapper safetyExaminationPaperInfoMapper;
    @Autowired
    private PrizeWinRecordMapper prizeWinRecordMapper;
    @Autowired
    private PrizeInfoMapper prizeInfoMapper;
    @Autowired
    private PrizeLuckMapper prizeLuckMapper;
    @Autowired
    private SafetyExaminationPaperquestionMapper safetyExaminationPaperquestionMapper;
    @Autowired
    private IConnectService connectService;
    /**
     * 查询考试基本信息
     *
     * @return 考试基本信息
     */
    public SafetyExaminationFrontInfo selectexaminationinfo(StudentInfo studentInfo){
            if(!checkNull(studentInfo.getStudentId())) {
            SafetyExaminationInfo safetyExaminationInfo = new SafetyExaminationInfo();
            safetyExaminationInfo.setStartTime(getStamp().toString());
            SafetyExaminationFrontInfo safetyExaminationFrontInfo;
                safetyExaminationFrontInfo = safetyExaminationFrontMapper.selectSafetyExaminationInfoDateList(safetyExaminationInfo);
                if(safetyExaminationFrontInfo!=null){
                    List<String> prizeName = merchantFrontMapper.examinationPrizeName(safetyExaminationFrontInfo);
                    safetyExaminationFrontInfo.setPrizeName(prizeName);
                    SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
                    safetyExaminationStudentRecord.setStudentId(studentInfo.getStudentId());
                    safetyExaminationStudentRecord.setSafetyExaminationId(safetyExaminationFrontInfo.getSafetyExaminationId());
                    List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
                    if(safetyExaminationStudentRecords.size()>0){
                        safetyExaminationFrontInfo.setExaminationStatus(safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType().intValue());
                    }
                    else {
                        safetyExaminationFrontInfo.setExaminationStatus(2);
                    }
                }
                else{
                    safetyExaminationFrontInfo = safetyExaminationFrontMapper.selectSafetyExaminationInfoList(safetyExaminationInfo);
                    if(safetyExaminationFrontInfo!=null){
                        List<String> prizeName = merchantFrontMapper.examinationPrizeName(safetyExaminationFrontInfo);
                        safetyExaminationFrontInfo.setPrizeName(prizeName);
                        if(Long.valueOf(safetyExaminationFrontInfo.getFinishTime())<getStamp()){
                            safetyExaminationFrontInfo.setSafetyExaminationType(2);
                        }
                        else{
                            safetyExaminationFrontInfo.setSafetyExaminationType(3);
                        }
                    }
                    else{
                        safetyExaminationFrontInfo = new SafetyExaminationFrontInfo();
                        safetyExaminationFrontInfo.setSafetyExaminationType(4);
                    }
                }
                safetyExaminationFrontInfo.setFinishTime(StampToString(Long.valueOf(safetyExaminationFrontInfo.getFinishTime())));
                safetyExaminationFrontInfo.setStartTime(StampToString(Long.valueOf(safetyExaminationFrontInfo.getStartTime())));
                return safetyExaminationFrontInfo;
        }
        else{
            return null;
        }
    }

    /**
     * 查询学生考试题目和统计记录
     *
     * @param frontUserInfo 查询信息
     * @return StudentExamination
     */
    public ExaminationFrontContent selectExaminationFrontContent(FrontUserInfo frontUserInfo){
        if(!checkNull(frontUserInfo.getExaminationId())&&!checkNull(frontUserInfo.getStudentId())&&!checkNull(frontUserInfo.getPaperId())){
            SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
            safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
            safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
            List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
            if(safetyExaminationStudentRecords.size()<=0){
                return null;
            }
            int soft = 0;
            frontUserInfo.setQuestionId(safetyExaminationStudentRecords.get(0).getCurrentQuestionId());
            ExaminationFrontContent examinationFrontContent=new ExaminationFrontContent();
            List<ExaminationFrontQuestionInfo> examinationFrontQuestionInfos = new ArrayList<>();
            if(safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType()==0){
                soft = safetyExaminationFrontMapper.selectQuestionSoft(frontUserInfo).size();
                examinationFrontContent = safetyExaminationFrontMapper.selectExaminationFrontContent(frontUserInfo);
                examinationFrontQuestionInfos = safetyExaminationFrontMapper.selectSafetyExaminationQuestionList(frontUserInfo);
            }
            else if(safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType()==3){
                soft = safetyExaminationFrontMapper.selectQuestionWrongSoft(frontUserInfo).size();
                examinationFrontContent = safetyExaminationFrontMapper.selectExaminationFrontContent(frontUserInfo);
                examinationFrontContent.setFalseAmount(safetyExaminationFrontMapper.selectExaminationFrontWrongfalse(frontUserInfo));
                examinationFrontQuestionInfos = safetyExaminationFrontMapper.selectSafetyExaminationWrongQuestionList(frontUserInfo);
            }
            if(soft <= 0){
                soft = 1;
            }

            examinationFrontContent.setCurrentSoft(soft-1);
            examinationFrontContent.setExaminationFrontQuestionInfos(examinationFrontQuestionInfos);
            return examinationFrontContent;
        }
        return null;
    }

    /**
     * 查询学生考试记录
     *
     * @param frontUserInfo 查询信息
     * @return int
     */
    public String selectStudentExaminationRecord(FrontUserInfo frontUserInfo){
        if(!checkNull(frontUserInfo.getExaminationId())&&!checkNull(frontUserInfo.getStudentId())){
            SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
            List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = new ArrayList<>();
            safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
            safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
            safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
            if(safetyExaminationStudentRecords.size()>0){
                safetyExaminationStudentRecord.setId(safetyExaminationStudentRecords.get(0).getId());
                if(safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType()==1){
                    if(safetyExaminationStudentRecords.get(0).getQuestionAlready() == safetyExaminationStudentRecords.get(0).getTrueAmount()){
                        return null;
                    }
                    safetyExaminationStudentRecord.setSafetyExaminationStudentType(3l);
                    safetyExaminationStudentRecord.setFinalDate(getStamp());
                    safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecord);
                    return safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId();
                }
                safetyExaminationStudentRecord.setFinalDate(getStamp());
                safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecord);
                return safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId();
            }
            else{
                Integer numsoft;
                SafetyExaminationExaminationpaper safetyExaminationExaminationpaper = new SafetyExaminationExaminationpaper();
                safetyExaminationExaminationpaper.setSafetyExaminationId(frontUserInfo.getExaminationId());
                List<SafetyExaminationExaminationpaper> safetyExaminationExaminationpapers = safetyExaminationExaminationpaperMapper.selectSafetyExaminationExaminationpaperList(safetyExaminationExaminationpaper);
                if(safetyExaminationExaminationpapers.size()>0){
                    numsoft = getStamp().intValue();
                    safetyExaminationStudentRecord.setSafetyExaminationPaperId(safetyExaminationExaminationpapers.get(numsoft % safetyExaminationExaminationpapers.size()).getSafetyExaminationPaperId());
                    safetyExaminationStudentRecord.setSafetyExaminationStudentType(0l);
                    safetyExaminationStudentRecord.setPrizeType(0l);
                    safetyExaminationStudentRecord.setStartDate(getStamp());
                    safetyExaminationStudentRecord.setFinalDate(getStamp());
                    safetyExaminationStudentRecord.setQuestionAlready(0l);
                    safetyExaminationStudentRecord.setTrueAmount(0l);
                    safetyExaminationStudentRecordMapper.insertSafetyExaminationStudentRecord(safetyExaminationStudentRecord);
                    return safetyExaminationStudentRecord.getSafetyExaminationPaperId();
                }
            }
        }
        return null;
    }

    /**
     * 考试保存
     *
     * @param frontUserInfo 题目信息
     * @return int
     */
    public int saveExamination(FrontUserInfo frontUserInfo){
        if(!checkNull(frontUserInfo.getStudentId())&&!checkNull(frontUserInfo.getExaminationId())&&!checkNull(frontUserInfo.getQuestionId())){
            SafetyExaminationQuestion safetyExaminationQuestion = new SafetyExaminationQuestion();
            safetyExaminationQuestion.setQuestionId(frontUserInfo.getQuestionId());
            //查询是否有选择答案传入
            if(!checkNull(frontUserInfo.getSelectAnwser())) {
                safetyExaminationQuestion.setQuestionAnswer(frontUserInfo.getSelectAnwser());
                if (safetyExaminationQuestionMapper.selectSafetyExaminationQuestionFrontList(safetyExaminationQuestion).size() <= 0)//查询答题是否正确
                {
                    SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
                    safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
                    safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                    List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
                    //查询本次考试学生记录
                    if (safetyExaminationStudentRecords.size() > 0) {
                        if (safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType() == 0) //判断学生考试状态（0：进行中，1：已完成，2：未考，3：重考）
                        {
                            //设置错题记录数据
                            SafetyExaminationStudentWrongRecord safetyExaminationStudentWrongRecord = new SafetyExaminationStudentWrongRecord();
                            safetyExaminationStudentWrongRecord.setQuestionId(frontUserInfo.getQuestionId());
                            safetyExaminationStudentWrongRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                            safetyExaminationStudentWrongRecord.setSafetyExaminationPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                            safetyExaminationStudentWrongRecord.setSelectRecord(frontUserInfo.getSelectAnwser());
                            safetyExaminationStudentWrongRecord.setStudentId(frontUserInfo.getStudentId());
                            //更新当前进度
                            frontUserInfo.setPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                            String questionId = safetyExaminationFrontMapper.selectQuestionSaveId(frontUserInfo);
                            //判断题目是否有下一个题目，若没有，结束考试
                            if (!checkNull(questionId)) {
                                safetyExaminationStudentRecords.get(0).setCurrentQuestionId(questionId);
                            } else {
                                safetyExaminationStudentRecords.get(0).setSafetyExaminationStudentType(1l);
                                safetyExaminationStudentRecords.get(0).setCurrentQuestionId("0");
                            }
                            safetyExaminationStudentRecords.get(0).setFinalDate(getStamp());
                            SafetyExaminationPaperquestion safetyExaminationPaperquestion = new SafetyExaminationPaperquestion();
                            safetyExaminationPaperquestion.setSafetyExaminationPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                            safetyExaminationPaperquestion.setQuestionId(frontUserInfo.getQuestionId());
                            safetyExaminationStudentRecords.get(0).setQuestionAlready(safetyExaminationPaperquestionMapper.selectSafetyExaminationPaperquestionList(safetyExaminationPaperquestion).get(0).getQuestionSoft()+1);
                            safetyExaminationStudentRecords.get(0).setTrueAmount(safetyExaminationFrontMapper.selectQuestionTrueAmount(frontUserInfo));
                            safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                            return safetyExaminationStudentWrongRecordMapper.insertSafetyExaminationStudentWrongRecord(safetyExaminationStudentWrongRecord);
                        } else if (safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType() == 3) {
                            //查询上次记录时间和当前题目的Id
                            List<Long> Ids = new ArrayList<>();
                            SafetyExaminationStudentWrongRecord safetyExaminationStudentWrongRecord = new SafetyExaminationStudentWrongRecord();
                            safetyExaminationStudentWrongRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                            safetyExaminationStudentWrongRecord.setStudentId(frontUserInfo.getStudentId());
                            safetyExaminationStudentWrongRecord.setQuestionId(safetyExaminationStudentRecords.get(0).getCurrentQuestionId());
                            List<SafetyExaminationStudentWrongRecord> safetyExaminationStudentWrongRecords = new ArrayList<>();
                            safetyExaminationStudentWrongRecords = safetyExaminationStudentWrongRecordMapper.selectSafetyExaminationStudentWrongRecordList(safetyExaminationStudentWrongRecord);
                            if(safetyExaminationStudentWrongRecords.size()>0) {
                                Ids.add(safetyExaminationStudentWrongRecords.get(0).getId());
                            }
                            else {
                                Ids.add(0l);
                            }
                            safetyExaminationStudentWrongRecord.setQuestionId(frontUserInfo.getQuestionId());
                            safetyExaminationStudentWrongRecord = safetyExaminationStudentWrongRecordMapper.selectSafetyExaminationStudentWrongRecordList(safetyExaminationStudentWrongRecord).get(0);
                            Ids.add(safetyExaminationStudentWrongRecord.getId()-1);
                            frontUserInfo.setIds(Ids);
                            safetyExaminationFrontMapper.deleteStudentWrongId(frontUserInfo);
                            safetyExaminationStudentWrongRecord.setSelectRecord(frontUserInfo.getSelectAnwser());
                            //更新当前进度
                            frontUserInfo.setPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                            String questionId = safetyExaminationFrontMapper.selectQuestionSaveWrongId(frontUserInfo);
                            if (!checkNull(questionId)) {
                                safetyExaminationStudentRecords.get(0).setCurrentQuestionId(questionId);
                            } else {
                                safetyExaminationStudentRecords.get(0).setSafetyExaminationStudentType(1l);
                                safetyExaminationStudentRecords.get(0).setCurrentQuestionId("0");
                            }
                            safetyExaminationStudentRecords.get(0).setFinalDate(getStamp());
                            safetyExaminationStudentRecords.get(0).setTrueAmount(safetyExaminationFrontMapper.selectQuestionTrueAmountWrong(frontUserInfo));
                            safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                            return safetyExaminationStudentWrongRecordMapper.updateSafetyExaminationStudentWrongRecord(safetyExaminationStudentWrongRecord);
                        }
                    }
                }
                else//题目答案正确
                {
                    SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
                    safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
                    safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                    List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
                    if (safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType() == 0) //判断学生考试状态（0：进行中，1：已完成，2：未考，3：重考）
                    {
                        //更新当前进度
                        frontUserInfo.setPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                        String questionId = safetyExaminationFrontMapper.selectQuestionSaveId(frontUserInfo);
                        //判断题目是否有下一个题目，若没有，结束考试
                        if (!checkNull(questionId)) {
                            safetyExaminationStudentRecords.get(0).setCurrentQuestionId(questionId);
                        } else {
                            safetyExaminationStudentRecords.get(0).setSafetyExaminationStudentType(1l);
                            safetyExaminationStudentRecords.get(0).setCurrentQuestionId("0");
                        }
                        safetyExaminationStudentRecords.get(0).setFinalDate(getStamp());
                        SafetyExaminationPaperquestion safetyExaminationPaperquestion = new SafetyExaminationPaperquestion();
                        safetyExaminationPaperquestion.setSafetyExaminationPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                        safetyExaminationPaperquestion.setQuestionId(frontUserInfo.getQuestionId());
                        safetyExaminationStudentRecords.get(0).setQuestionAlready(safetyExaminationPaperquestionMapper.selectSafetyExaminationPaperquestionList(safetyExaminationPaperquestion).get(0).getQuestionSoft()+1);
                        safetyExaminationStudentRecords.get(0).setTrueAmount(safetyExaminationFrontMapper.selectQuestionTrueAmount(frontUserInfo)+1);
                        return safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                    } else if (safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType() == 3) {
                        //查询上次记录时间和当前题目的Id
                        List<Long> Ids = new ArrayList<>();
                        SafetyExaminationStudentWrongRecord safetyExaminationStudentWrongRecord = new SafetyExaminationStudentWrongRecord();
                        safetyExaminationStudentWrongRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                        safetyExaminationStudentWrongRecord.setStudentId(frontUserInfo.getStudentId());
                        safetyExaminationStudentWrongRecord.setQuestionId(safetyExaminationStudentRecords.get(0).getCurrentQuestionId());
                        List<SafetyExaminationStudentWrongRecord> safetyExaminationStudentWrongRecords = new ArrayList<>();
                        safetyExaminationStudentWrongRecords = safetyExaminationStudentWrongRecordMapper.selectSafetyExaminationStudentWrongRecordList(safetyExaminationStudentWrongRecord);
                        if(safetyExaminationStudentWrongRecords.size()>0) {
                            Ids.add(safetyExaminationStudentWrongRecords.get(0).getId());
                        }
                        else {
                            Ids.add(0l);
                        }
                        safetyExaminationStudentWrongRecord.setQuestionId(frontUserInfo.getQuestionId());
                        safetyExaminationStudentWrongRecord = safetyExaminationStudentWrongRecordMapper.selectSafetyExaminationStudentWrongRecordList(safetyExaminationStudentWrongRecord).get(0);
                        Ids.add(safetyExaminationStudentWrongRecord.getId());
                        frontUserInfo.setIds(Ids);
                        safetyExaminationFrontMapper.deleteStudentWrongId(frontUserInfo);
                        safetyExaminationStudentWrongRecord.setSelectRecord(frontUserInfo.getSelectAnwser());
                        //更新当前进度
                        frontUserInfo.setPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                        String questionId = safetyExaminationFrontMapper.selectQuestionSaveWrongId(frontUserInfo);
                        if (!checkNull(questionId)) {
                            safetyExaminationStudentRecords.get(0).setCurrentQuestionId(questionId);
                        } else {
                            safetyExaminationStudentRecords.get(0).setSafetyExaminationStudentType(1l);
                            safetyExaminationStudentRecords.get(0).setCurrentQuestionId("0");
                        }
                        safetyExaminationStudentRecords.get(0).setFinalDate(getStamp());
                        safetyExaminationStudentRecords.get(0).setTrueAmount(safetyExaminationFrontMapper.selectQuestionTrueAmountWrong(frontUserInfo));
                        safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                        return safetyExaminationStudentWrongRecordMapper.updateSafetyExaminationStudentWrongRecord(safetyExaminationStudentWrongRecord);
                    }
                }
            }
            else{
                SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
                safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
                safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
                //查询本次考试学生记录
                if(safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType() == 0) //判断学生考试状态（0：进行中，1：已完成，2：未考，3：重考）
                {
                    //更新当前进度
                    safetyExaminationStudentRecords.get(0).setCurrentQuestionId(frontUserInfo.getQuestionId());
                    safetyExaminationStudentRecords.get(0).setFinalDate(getStamp());
                    SafetyExaminationPaperquestion safetyExaminationPaperquestion = new SafetyExaminationPaperquestion();
                    safetyExaminationPaperquestion.setSafetyExaminationPaperId(safetyExaminationStudentRecords.get(0).getSafetyExaminationPaperId());
                    safetyExaminationPaperquestion.setQuestionId(frontUserInfo.getQuestionId());
                    safetyExaminationStudentRecords.get(0).setQuestionAlready(safetyExaminationPaperquestionMapper.selectSafetyExaminationPaperquestionList(safetyExaminationPaperquestion).get(0).getQuestionSoft());
                    safetyExaminationStudentRecords.get(0).setTrueAmount(safetyExaminationFrontMapper.selectQuestionTrueAmount(frontUserInfo)+1);
                    return safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                }
                else if(safetyExaminationStudentRecords.get(0).getSafetyExaminationStudentType()==3){
                    //查询上次记录时间和当前题目的Id
                    List<Long> Ids = new ArrayList<>();
                    Ids.add(safetyExaminationStudentRecords.get(0).getId()-1);
                    SafetyExaminationStudentWrongRecord safetyExaminationStudentWrongRecord = new SafetyExaminationStudentWrongRecord();
                    safetyExaminationStudentWrongRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                    safetyExaminationStudentWrongRecord.setStudentId(frontUserInfo.getStudentId());
                    safetyExaminationStudentWrongRecord.setQuestionId(safetyExaminationStudentRecords.get(0).getCurrentQuestionId());
                    List<SafetyExaminationStudentWrongRecord> safetyExaminationStudentWrongRecords = new ArrayList<>();
                    safetyExaminationStudentWrongRecords = safetyExaminationStudentWrongRecordMapper.selectSafetyExaminationStudentWrongRecordList(safetyExaminationStudentWrongRecord);
                    if(safetyExaminationStudentWrongRecords.size()>0) {
                        Ids.add(safetyExaminationStudentWrongRecords.get(0).getId());
                    }
                    else {
                        Ids.add(0l);
                    }
                    safetyExaminationStudentWrongRecord.setQuestionId(frontUserInfo.getQuestionId());
                    safetyExaminationStudentWrongRecord = safetyExaminationStudentWrongRecordMapper.selectSafetyExaminationStudentWrongRecordList(safetyExaminationStudentWrongRecord).get(0);
                    Ids.add(safetyExaminationStudentWrongRecord.getId()-1);
                    frontUserInfo.setIds(Ids);
                    safetyExaminationFrontMapper.deleteStudentWrongId(frontUserInfo);
                    //更新当前进度
                    safetyExaminationStudentRecords.get(0).setCurrentQuestionId(frontUserInfo.getSelectAnwser());
                    safetyExaminationStudentRecords.get(0).setFinalDate(getStamp());
                    safetyExaminationStudentRecords.get(0).setTrueAmount(safetyExaminationFrontMapper.selectQuestionTrueAmountWrong(frontUserInfo));
                    return safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                }
            }
        }
        return 0;
    }

    /**
     * 学生成绩统计
     *
     * @param frontUserInfo 学生信息
     * @return int
     */
    public ExaminationFrontResult selectExaminationFrontResult(FrontUserInfo frontUserInfo){
        if(!checkNull(frontUserInfo.getStudentId())&&!checkNull(frontUserInfo.getExaminationId())){
            SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
            safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
            safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
            if(safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord).get(0).getSafetyExaminationStudentType()==1){
                ExaminationFrontResult examinationFrontResult = safetyExaminationFrontMapper.selectExaminationFrontResult(frontUserInfo);
                if(examinationFrontResult!=null){
                    examinationFrontResult.setExaminationFrontPrize(safetyExaminationFrontMapper.selectExaminationFrontPrize(frontUserInfo));
                    return examinationFrontResult;
                }
            }
        }
        return null;
    }

    /**
     * 抽奖
     *
     * @param frontUserInfo 抽奖信息
     * @return 奖品号
     */
    public ExaminationFrontPrize luckDraw(FrontUserInfo frontUserInfo){
        if(!checkNull(frontUserInfo.getStudentId())&&!checkNull(frontUserInfo.getExaminationId())){
            ExaminationFrontResult examinationFrontResult = safetyExaminationFrontMapper.selectExaminationFrontResult(frontUserInfo);
            if(examinationFrontResult.getPass()&&examinationFrontResult.getPrizeType()){
                List<ExaminationFrontPrize> examinationFrontPrizes = safetyExaminationFrontMapper.selectExaminationFrontPrize(frontUserInfo);
                Random random = new Random();
                float randomFloat = random.nextFloat();
                float backInt = 0;
                SafetyExaminationStudentRecord safetyExaminationStudentRecord = new SafetyExaminationStudentRecord();
                safetyExaminationStudentRecord.setStudentId(frontUserInfo.getStudentId());
                safetyExaminationStudentRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                List<SafetyExaminationStudentRecord> safetyExaminationStudentRecords = safetyExaminationStudentRecordMapper.selectSafetyExaminationStudentRecordList(safetyExaminationStudentRecord);
                if(safetyExaminationStudentRecords.size()>0){
                    for(ExaminationFrontPrize examinationFrontPrize:examinationFrontPrizes){
                        if(examinationFrontPrize.getPrizeAmout()>0){
                            backInt += examinationFrontPrize.getPrizeProbability();
                            if (backInt > randomFloat) {
                                safetyExaminationStudentRecords.get(0).setPrizeType(1l);
                                PrizeInfo prizeInfo = new PrizeInfo();
                                prizeInfo.setPrizeId(examinationFrontPrize.getPrizeId());
                                List<PrizeInfo> prizeInfos = prizeInfoMapper.selectPrizeInfoList(prizeInfo);
                                if (prizeInfos.size() > 0) {
                                    PrizeLuck prizeLuck = new PrizeLuck();
                                    prizeLuck.setPrizeId(examinationFrontPrize.getPrizeId());
                                    prizeLuck.setSafetyExaminationId(frontUserInfo.getExaminationId());
                                    List<PrizeLuck> prizeLucks = prizeLuckMapper.selectPrizeLuckList(prizeLuck);
                                    if (prizeLucks.size() > 0) {
                                        if (prizeLucks.get(0).getPrizeAmount() >= 1 && prizeInfos.get(0).getPrizeAmount() >= 1) {
                                            prizeLucks.get(0).setPrizeAmount(prizeLucks.get(0).getPrizeAmount() - 1);
                                            prizeInfos.get(0).setPrizeAmount(prizeInfos.get(0).getPrizeAmount() - 1);
                                            PrizeWinRecord prizeWinRecord = new PrizeWinRecord();
                                            prizeWinRecord.setPrizeWinDate(getStamp().toString());
                                            prizeWinRecord.setPrizeId(examinationFrontPrize.getPrizeId());
                                            prizeWinRecord.setReceiveType("0");
                                            prizeWinRecord.setSafetyExaminationId(frontUserInfo.getExaminationId());
                                            prizeWinRecord.setStudentId(frontUserInfo.getStudentId());
                                            prizeWinRecord.setPrizeWinPic(connectService.PQcreate(prizeWinRecord));
                                            prizeLuckMapper.updatePrizeLuck(prizeLucks.get(0));
                                            prizeInfoMapper.updatePrizeInfo(prizeInfos.get(0));
                                            prizeWinRecordMapper.insertPrizeWinRecord(prizeWinRecord);
                                            safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                                            return examinationFrontPrize;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    ExaminationFrontPrize examinationFrontPrize = new ExaminationFrontPrize();
                    examinationFrontPrize.setPrizeId("0");
                    if(safetyExaminationStudentRecords.get(0).getPrizeType()!=1l) {
                        safetyExaminationStudentRecords.get(0).setPrizeType(2l);
                    }
                    safetyExaminationStudentRecordMapper.updateSafetyExaminationStudentRecord(safetyExaminationStudentRecords.get(0));
                    return examinationFrontPrize;
                }

            }
        }
        return null;
    }
}
